package com.ihr360.alps;

import com.ihr360.alps.api.AlpsProvider;
import com.ihr360.alps.api.ServiceProvider;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.payroll.service.alps.profile.AttendanceServiceProfile;
import com.ihr360.payroll.service.alps.profile.EnrollDismissAdjustServiceProfile;
import com.ihr360.payroll.service.alps.profile.OverTimeRuleServiceProfile;
import com.ihr360.payroll.service.alps.profile.PersonLeaveServiceProfile;
import com.ihr360.payroll.service.alps.profile.SalaryAdjustServiceProfile;
import com.ihr360.payroll.service.alps.profile.SickLeaveServiceProfile;
import com.ihr360.payroll.service.alps.profile.StaffSalaryServiceProfile;
import com.ihr360.payroll.service.attendancereport.AttendanceMonthlyReportService;
import com.ihr360.payroll.service.attendancereport.AttendanceMonthlyReportSummaryService;
import com.ihr360.payroll.service.salaryprofile.CompanySalaryProfileService;
import com.ihr360.payroll.service.salarytax.SalaryTaxTableService;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("payrollAlpsProvider")
@RequestMapping(value = "/alps")
@DisableResponseAdvice
public class PayrollAlpsProvider implements AlpsProvider {

    @Autowired
    private SalaryTaxTableService salaryTaxTableService;

    @Autowired
    private AttendanceMonthlyReportService attendanceMonthlyReportService;

    @Autowired
    private AttendanceMonthlyReportSummaryService attendanceMonthlyReportSummaryService;

    @Autowired
    private CompanySalaryProfileService companySalaryProfileService;


    @Value("${spring.application.name}")
    private String appId;

    @GetMapping(value = "/application", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ApplicationDescriptor applicationDescriptor() {

        List<ServiceDescriptor> descriptors = new ArrayList<>(6);
        ServiceDescriptor overtime = serviceDescriptor(ServiceProvider.SERVICE_ID.SALARY_OT_RULE);
        if (overtime != null) {
            descriptors.add(overtime);
        }

        ServiceDescriptor sickleave = serviceDescriptor(ServiceProvider.SERVICE_ID.SALARY_SL_RULE);
        if (sickleave != null) {
            descriptors.add(sickleave);
        }
        ServiceDescriptor personalLeave = serviceDescriptor(ServiceProvider.SERVICE_ID.SALARY_PL_RULE);
        if (personalLeave != null) {
            descriptors.add(personalLeave);
        }

        ServiceDescriptor adjust = serviceDescriptor(ServiceProvider.SERVICE_ID.SALARY_ADJUST_RULE);
        if (adjust != null) {
            descriptors.add(adjust);
        }

        ServiceDescriptor edAdjust = serviceDescriptor(ServiceProvider.SERVICE_ID.SALARY_ENTRY_DISMISS_ADJUST_RULE);
        if (edAdjust != null) {
            descriptors.add(edAdjust);
        }

        ServiceDescriptor salarytable = serviceDescriptor(ServiceProvider.SERVICE_ID.SYS_TAX_TABLE);
        if (salarytable != null) {
            descriptors.add(salarytable);
        }

        ServiceDescriptor attendance = serviceDescriptor(ServiceProvider.SERVICE_ID.ATTENDANCE_MONTHLY_REPORT);
        if (attendance != null) {
            descriptors.add(attendance);
        }
        ServiceDescriptor staffsalary = serviceDescriptor(ServiceProvider.SERVICE_ID.STAFF_SALARY);
        if (staffsalary != null) {
            descriptors.add(staffsalary);
        }


        ApplicationDescriptor applicationDescriptor = ApplicationDescriptor.builder()
                .name("薪资云")
                .services(descriptors)
                .id(appId).build();
        return applicationDescriptor;
    }

    @GetMapping(value = "/services/{service}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ServiceDescriptor serviceDescriptor(@PathVariable(name = "service") String serviceId) {

        switch (serviceId) {
            case ServiceProvider.SERVICE_ID.SALARY_OT_RULE:
                return OverTimeRuleServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.SALARY_SL_RULE:
                return SickLeaveServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.SALARY_PL_RULE:
                return PersonLeaveServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.SALARY_ADJUST_RULE:
                return SalaryAdjustServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.SALARY_ENTRY_DISMISS_ADJUST_RULE:
                return EnrollDismissAdjustServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.SYS_TAX_TABLE:
                return salaryTaxTableService.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.ATTENDANCE_MONTHLY_REPORT:
                return AttendanceServiceProfile.serviceDescriptor();
            case ServiceProvider.SERVICE_ID.STAFF_SALARY:
                return StaffSalaryServiceProfile.serviceDescriptor();
            default:
                return null;
        }
    }


    @Override
    @PostMapping(value = "/companyService/{service}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceDescriptor companyServiceDescriptor(@PathVariable("service") String serviceId,
                                                      @RequestParam("companyId") String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            throw new Ihr360Exception(null, "公司信息为空");
        }
        switch (serviceId) {
            case ServiceProvider.SERVICE_ID.ATTENDANCE_MONTHLY_REPORT:
                return attendanceMonthlyReportSummaryService.serviceDescriptor(companyId);
            case ServiceProvider.SERVICE_ID.STAFF_SALARY:
                return companySalaryProfileService.serviceDescriptor(companyId);
            default:
                return new ServiceDescriptor();
        }
    }
}